import { AxiosInstance, AxiosPromise, AxiosRequestConfig } from "axios";
import QueryString from "qs";

export function setRequestObj(vAxiosObj: AxiosInstance) {
  if (vAxiosObj) {
    NetworkUtil._axios = vAxiosObj;
  } else {
    console.error("setting axios is undefined");
  }
}
export default class NetworkUtil {
  static _axios: AxiosInstance;
  static request<T>(config: AxiosRequestConfig): AxiosPromise<T> {
    return NetworkUtil._axios!.request<T>(config);
  }
  /**
   * get请求
   * @param url {string}
   * @param data? {Object as any}
   */
  static getReq(url: string, data?: any): Promise<any> {
    return NetworkUtil.request({ data: data || {}, url: url, method: "get" });
  }

  /**
   * get请求 传token
   * @param Authorization-token  {string}
   * @param url {string}
   * @param data? {Object as any}
   */
  static getReqByToken(token: string, url: string, data?: any): Promise<any> {
    const cfg: any = {
      url: url,
      headers: {
        Authorization: token,
      },
    };
    if (data) {
      cfg.data = data;
    }
    return NetworkUtil.request(cfg);
  }

  /**
   * post 请求
   * @param url {string}
   * @param data {Object as any}
   */
  static postReq(url: string, data: any): Promise<any> {
    console.log("url:", url, "data:", data);
    return NetworkUtil.request({
      data: data,
      url: url,
      method: "post",
    });
  }

  /**
   * get 请求使用head
   * @param uri
   * @param headers
   * @param data
   */
  static getReqByHeads(
    uri: string,
    headers: { [key: string]: string },
    data?: any
  ) {
    const cfg: any = {
      url: uri,
      method: "GET",
    };
    if (headers && Object.keys(headers).length > 0) {
      cfg.headers = headers;
    }
    if (data) {
      cfg.data = data;
    }

    return NetworkUtil.request(cfg);
  }

  /**
   * post 请求，使用header
   * @param uri
   * @param headers { { [key: string]: string } }
   * @param data
   */
  static postReqByHeads<T>(
    uri: string,
    headers: { [key: string]: string },
    data?: any
  ): AxiosPromise<T> {
    console.log(`【reqUrl】:${uri}\n【headers】:${JSON.stringify(
      headers
    )}\n【data】:${JSON.stringify(data)}
    `);
    const cfg: any = {
      url: uri,
      method: "POST",
    };
    if (headers && Object.keys(headers).length > 0) {
      cfg.headers = headers;
    }
    if (data) {
      cfg.data = data;
    }
    if (
      headers["Content-Type"] &&
      headers["Content-Type"].indexOf("application/x-www-form-urlencoded") >= 0
    ) {
      cfg.transformRequest = [
        () => {
          return QueryString.stringify(data);
        },
      ];
    }

    return NetworkUtil.request<T>(cfg);
  }

  /**
   * 使用auth方式请求
   * @param url
   * @param username
   * @param password
   * @param data？
   */
  static authReq(
    url: string,
    username: string,
    password: string,
    data?: any
  ): Promise<any> {
    return NetworkUtil._axios.request({
      url: url,
      method: "get",
      auth: {
        username: username,
        password: password,
      },
      data: data || {},
    });
  }
}
